package br.com.jw.dao;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.jw.jdbc.Pool;
import br.com.jw.modelo.Usuario;

public class UsuarioDao{
	private Pool pool;
	Connection conn;
	PreparedStatement stmt; 
	int resultado = 0;
	boolean aux = false;
public UsuarioDao(Pool pool){
	this.pool=pool;
}
	

	public static String criptografar(String senha) {
		    try {
		        MessageDigest digest = MessageDigest.getInstance("SHA-256");
		        byte[] msg = digest.digest(senha.getBytes("UTF-8"));
		        StringBuilder hexString = new StringBuilder();
		        for (byte b : msg) {
		            hexString.append(String.format("%02X", 0xFF & b));
		        }
		        return hexString.toString();
		    } catch (NoSuchAlgorithmException ne) {
		        ne.printStackTrace();
		    } catch (UnsupportedEncodingException ue) {
		        ue.printStackTrace();
		    }
		    return null;
		}

	public boolean alterarUsuario(Usuario usuario) throws ClassNotFoundException, SQLException{

		String sql = "update usuario set usu_login=?, usu_senha=? where usu_id=?";
		conn=pool.getConnection();
		try {
			stmt = conn.prepareStatement(sql);
						
			stmt.setString(1, usuario.getUsu_login());
			stmt.setString(2, criptografar(usuario.getUsu_senha()));
			stmt.setInt(3, usuario.getUsu_id());
			
		    stmt.execute();
		    
		    stmt.close();
			conn.close();
			return true;
			
		} finally{
			pool.liberarConexao(conn);
			
		}
		}
	public int logar(Usuario usuario) throws ClassNotFoundException, SQLException {
	
		//int resultado=0;
		String sql = "select * from usuario where usu_login=? and usu_senha=?";
		
		try {
			conn =pool.getConnection();
			stmt = conn.prepareStatement(sql);

			stmt.setString(1, usuario.getUsu_login());
			stmt.setString(2, criptografar(usuario.getUsu_senha()));
			ResultSet rs = stmt.executeQuery();
			
			if(rs.next()){
				resultado=rs.getInt("usu_id");
			}
			
			stmt.close();
			rs.close();
			return resultado;
				
		} finally {
			pool.liberarConexao(conn);
			}
		
		}
					

	public boolean inserirUsuario(Usuario usuario) throws ClassNotFoundException, SQLException {
	
		String sql = "insert into usuario (usu_login,usu_senha)values (?,?)";
		conn = pool.getConnection();
		try {
			stmt = conn.prepareStatement(sql);

			stmt.setString(1, usuario.getUsu_login());
			stmt.setString(2, criptografar(usuario.getUsu_senha()));
          
			stmt.execute();		
			
			stmt.close();
			return true;
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			pool.liberarConexao(conn);
		}
		return false;
	}
	
	public List<Usuario> getUsuarios() throws ClassNotFoundException, SQLException{
		
		List<Usuario> listaUsuarios=new ArrayList<>();
		String sql="select * from usuario";
		conn = pool.getConnection();
		try{
			try {
				stmt=conn.prepareStatement(sql);
				ResultSet rs=stmt.executeQuery();
				while(rs.next()){
					listaUsuarios.add(new Usuario(
							rs.getInt("usu_id"),
							rs.getString("usu_login"),
							rs.getString("usu_senha")
							));				
				}
					
					stmt.close();
					rs.close();
					return listaUsuarios;
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}finally{
			pool.liberarConexao(conn);
		}
		return null;
	}

	public boolean excluirUsuario(int usu_id) throws ClassNotFoundException, SQLException {
		String sql = "delete from usuario where usu_id=?";
		conn = pool.getConnection();
		try {
			stmt = conn.prepareStatement(sql);

			stmt.setInt(1, usu_id);

		    stmt.execute();		    
		    stmt.close();
			conn.close();
			
			return true;
		} finally {    
			pool.liberarConexao(conn);
		}
			
	}
}
